package com.engine;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.io.File;

import javax.swing.JPanel;

import com.ui.Jamsostek;

import de.humatic.dsj.DSFiltergraph;
import de.humatic.dsj.DSJUtils;
import de.humatic.dsj.DSMovie;
import de.humatic.dsj.SwingMovieController;

public class DVDMovie extends JPanel implements Runnable,java.beans.PropertyChangeListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DSFiltergraph movie;
	private String []strFiles;
	private int idx=0;
	private SwingMovieController sc;
	private boolean auto_read=false;
	public DVDMovie()
	{
		int countVideo=ReadConfig.toInt("VIDEO_COUNTER");
		String[] videoItems =ReadConfig.GET("VIDEO_FILE").split("\\|");
		
		auto_read=Boolean.parseBoolean(ReadConfig.GET("AUTO_READ"));
		if(auto_read)
		{
			File dir = new File("video");
			String[] children = dir.list();
			if (children == null) {
			    // Either dir does not exist or is not a directory
			} else {
				countVideo=children.length;
				videoItems=new String[countVideo];
			    for (int i=0; i<children.length; i++) {
			        // Get filename of file or directory
			        String filename = children[i];
			        videoItems[i]=filename;
			    }
			}
		}
		if(countVideo<0) return;
		float volumeVideo=Float.parseFloat(ReadConfig.GET("VOLUME"));
		
		strFiles=new String[countVideo];
		for(int i=0; i < countVideo;i++)
		{
			strFiles[i] = "video/" + videoItems[i]; 
		}
		
		idx=0;
		while(!isFileExist(strFiles[idx]))
		{
			idx++;
		}
		movie = new DSMovie(strFiles[idx], DSFiltergraph.D3D9, this);
		movie.setPreferredSize(this.getPreferredSize());
		movie.setMaximumSize(this.getMaximumSize());
		try
		{
			movie.setVolume(volumeVideo);
		}
		catch(Exception ex){}
		this.setLayout(new BorderLayout());
		this.add(movie,BorderLayout.CENTER);
		movie.play();
		sc=new SwingMovieController(movie);
		//this.add(sc,BorderLayout.SOUTH);
	}
	
	private boolean isFileExist(String filename)
	{
		File file=new File(filename);
		return file.exists();
	}
	public void run()
	{
		try
		{
			idx++;
			if(idx==strFiles.length)
				idx=0;
			while(!isFileExist(strFiles[idx]))
			{
				idx++;
			}
			movie.dispose();
			movie = DSFiltergraph.createDSFiltergraph(strFiles[idx], DSFiltergraph.D3D9,
					this);
			movie.setPreferredSize(this.getPreferredSize());
			movie.setMaximumSize(this.getMaximumSize());
			//movie= new DSMovie(strFiles[idx], DSFiltergraph.DD7, this);
			sc.setFiltergraph(movie);
			float volumeVideo=Float.parseFloat(ReadConfig.GET("VOLUME"));
			try
			{
				movie.setVolume(volumeVideo);
			}
			catch(Exception ex){}
			this.add(movie,BorderLayout.CENTER);
			this.setVisible(true);
			movie.setVisible(true);
			movie.play();
			Jamsostek.frame.setVisible(true);
			System.out.println("Playing: " + strFiles[idx]);
		}
		catch(Exception ex)
		{
            System.out.println(ex.getLocalizedMessage());
		}
		
	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println(DSJUtils.getEventType(arg0));
		switch(DSJUtils.getEventType(arg0))
		{
			case de.humatic.dsj.DSConstants.EC_COMPLETE:
				break;
			case de.humatic.dsj.DSConstants.EC_PALETTE_CHANGED:
				//movie.stop();
				//movie.dispose();
				this.remove(movie);
				new Thread(this).start();
				break;
			case de.humatic.dsj.DSConstants.EC_SHUTTING_DOWN:
				break;
			case de.humatic.dsj.DSConstants.EC_QUALITY_CHANGE:
				break;
			case de.humatic.dsj.DSConstants.EC_DISPLAY_CHANGED:
				break;
		}
	}
}
